<template>
  <AMISRenderer :schema="schema" />
</template>

<script setup>
import AMISRenderer from '@/components/AMISRenderer.vue'

import addPage from './add'
import editPage from './edit'

const schema = {
  type:'page',
  body: [
  {
    type: 'crud',
    name:'menu-list',
    autoFillHeight: true,
    syncLocation: true,
    api: {
      url: '/upms/admin/menu/list/tree?systemId=${systemId}',
      sendOn: 'systemId'
    },
    footerToolbar: [
    ],
    autoGenerateFilter: {
      showBtnToolbar: false,
      columnsNum: 4
    },
    headerToolbar:[
      'bulkActions',
      {
        type: 'button',
        actionType: 'dialog',
        align: 'right',
        label: '复制到',
        dialog: {
          title: '选择目标系统',
          body: [
            {
              type: 'form',
              name: 'copyTo-form',
              api: {
                method: 'post',
                url: '/upms/admin/menu/copyTo',
                data:{
                  sourceSystemId: '${systemId}',
                  targetSystemId: '${targetSystemId}'
                }
              },
              body:[
                {
                  type: 'select',
                  name: 'targetSystemId',
                  label: '子系统',
                  source: {
                    url: '/upms/admin/system/select',
                    adaptor: function (payload, response, api, context) {
                      if(payload.data && payload.data.length > 0){
                        for(let i = 0; i < payload.data.length; i++){
                          payload.data[i].disabledOn = '${systemId == "' + payload.data[i].value + '"}';
                        }
                      }
                      return {
                          ...payload
                      };
                    }
                  },
                  required: true,
                  validationErrors: {
                    isRequired: '请选择目标系统'
                  }
                }
              ]
            }
          ],
          closeOnEsc: true,
          showErrorMsg: false,
        }
      },
      {
        type: 'button',
        actionType: 'dialog',
        align: 'right',
        label: '新增',
        icon: 'fa fa-plus pull-left',
        primary: true,
        dialog: {
          title: '新增菜单',
          body: addPage,
          closeOnEsc: true,
          showErrorMsg: false,
          size: 'md'
        }
      },
    ],
    bulkActions: [
      {
        label: '批量删除',
        actionType: 'ajax',
        api: 'post:/upms/admin/menu/deleteBatch?ids=${ids}',
        confirmText: '确定要批量删除?'
      }
    ],
    columns: [
      {
        name: 'systemName',
        label: '所属子系统',
        searchable: {
          type: 'select',
          name: 'systemId',
          label: '所属子系统',
          source: '/upms/admin/system/select',
          clearable: false,
          selectFirst: true,
        }
      },
      {
        name: 'menuName',
        label: '菜单名称',
        searchable: {
          type: 'input-text',
          name: 'menuName',
          label: '菜单名称',
          clearable: true
        }
      },
      {
        name: 'menuCode',
        label: '菜单编码',
        searchable: {
          type: 'input-text',
          name: 'menuCode',
          label: '菜单编码',
          clearable: true
        }
      },
      {
        name: 'path',
        label: '菜单路径'
      },
      {
        name: 'permission',
        label: '权限编码'
      },
      {
        name: 'typeStr',
        label: '菜单类型'
      },
      {
        name: 'sortNo',
        label: '排序号'
      },
      {
        name: 'visibleFlag',
        label: '是否显示',
        type: 'mapping',
        map: {
          "0": "<span class='label label-warning'>否</span>",
          "1": "<span class='label label-success'>是</span>",
        }
      },
      {
        type: 'operation',
        label: '操作',
        buttons: [
          {
            label: '修改',
            type: 'button',
            actionType: 'dialog',
            dialog:{
              title: '修改菜单',
              closeOnEsc: true,
              showErrorMsg: false,
              size: 'md',
              body: editPage
            }
          },
          {
            label: '删除',
            type: 'button',
            level: 'danger',
            actionType:'ajax',
            confirmText: '确认要删除该记录？',
            api: {
              url: '/upms/admin/menu/deleteBatch?ids=${id}',
              method: 'post',
              data:{}
            },
          }
        ]
      }
    ]
  }]
}

</script>

